<template>
  <el-dialog :visible.sync="dialog" :title="isAdd ? '新增规格' : '编辑规格'" append-to-body width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item label="规格名称：" prop="name">
        <el-input v-model="form.name" style="width: 200px;"/>
      </el-form-item>
      <el-form-item label="排序：" prop="sort">
        <el-input v-model.number="form.sort" style="width: 200px;"/>
      </el-form-item>
      <el-form-item
        label="可选项：">
        <el-input v-model="form.value" style="width: 200px;"/>
        <i class="el-icon-circle-plus" @click="addValue()"/>
      </el-form-item>
      <el-form-item
        v-for="(domain, index) in form.domains"
        :key="domain.key"
        :prop="'domains.' + index + '.value'">
        <el-input v-model="domain.value" style="width: 200px;"/>
        <i class="el-icon-remove" @click.prevent="removeValue(domain)"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="cancel">取消</el-button>
      <el-button :loading="loading" size="small" type="primary" @click="doSubmit('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/mall/specs'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    const checkSort = (rule, sort, callback) => {
      if (sort === '' || sort === undefined) {
        callback()
      } else if (!/^[0-9]+$/.test(sort) || sort === 0) {
        callback(new Error('排序必须是大于0的整数'))
      } else {
        callback()
      }
    }
    return {
      dialog: false,
      loading: false,
      form: {
        name: '',
        sort: '',
        value: '',
        specs_id: '',
        values: '',
        domains: []
      },
      rules: {
        name: [
          { required: true, message: '请输入规格名称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        sort: [
          { validator: checkSort, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    addValue() {
      this.form.domains.push({
        value: '',
        key: Date.now()
      })
    },
    removeValue(item) {
      const index = this.form.domains.indexOf(item)
      if (index !== -1) {
        this.form.domains.splice(index, 1)
      }
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = { name: '', sort: '', value: '', specs_id: '', domains: [], values: '' }
    },
    cancel() {
      this.resetForm()
    },
    doSubmit(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    doAdd() {
      const _values = []
      this.form.domains.forEach(item => {
        if (item.value !== '') {
          _values.push(item.value)
        }
      })
      this.form.values = _values
      add(this.form).then(res => {
        this.resetForm()
        if (res.result) {
          this.$notify({
            title: res.msg,
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.init()
        } else {
          this.$notify({
            title: res.msg,
            type: 'error',
            duration: 2500
          })
        }
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      const _values = []
      this.form.domains.forEach(item => {
        if (item.value !== '') {
          _values.push(item.value)
        }
      })
      this.form.values = _values
      edit(this.form).then(res => {
        this.resetForm()
        if (res.result) {
          this.$notify({
            title: res.msg,
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.init()
        } else {
          this.$notify({
            title: res.msg,
            type: 'error',
            duration: 2500
          })
        }
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
